<?php
declare(strict_types=1);

namespace App\Http\Request\Api\v1\Passport;

use App\Utils\AppResult;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class RegisterRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(): array
    {
        return [
            'account'  => 'required|string',
            'password' => 'required|string',
            'nickname' => 'nullable|string',
            'sex'      => 'nullable|integer',
            'avatar'   => 'nullable|string',
            'birthday' => 'nullable|string',
        ];
    }

    /**
     * 自定义返回信息
     * @return string[]
     */
    public function messages()
    {
        return [
            'account.required'  => '账号不能为空',
            'password.required' => '密码不能为空',
        ];
    }

    /**
     * 字段名称
     * @return string[]
     */
    public function attributes()
    {
        return [
            'account'  => '账号',
            'password' => '密码',
            'nickname' => '昵称',
            'sex'      => '性别',
            'avatar'   => '头像',
            'birthday' => '生日',
        ];
    }

    /**
     * 接管校验错误信息
     * @param \Illuminate\Contracts\Validation\Validator $validation
     */
    public function failedValidation($validation)
    {
        throw new HttpResponseException(AppResult::response203($validation->errors()->first()));
    }
}
